package com.example.blog.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.blog.mapper.SendHomeWorkMapper;
import com.example.blog.pojo.Result;
import com.example.blog.pojo.SendHomeWork;
import com.example.blog.service.SendHomeWorkService;
import com.example.blog.util.TokenUtil.decryptToken;
import com.example.blog.util.TokenUtil.spiltUtils;
import com.example.blog.util.UserUtils.UserUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;

@Service
@Slf4j
public class SendHomeWorkServiceImpl extends ServiceImpl<SendHomeWorkMapper, SendHomeWork> implements SendHomeWorkService {
    @Resource
    private UserUtils userIdUtil;
    @Resource
    private SendHomeWorkMapper sendHomeWorkMapper;

    @Override
    public Result<T> sendHomeWork(SendHomeWork sendHomeWork, int homeworkId, String token) {
        String pe = decryptToken.decrypt(token);
        String userName = spiltUtils.getUserName(pe);
        String nickname = userIdUtil.getNickname(userName);
        String headerUrl = userIdUtil.getHeaderUrl(userName);
        Integer userId = userIdUtil.getUserId(userName);
        int identity = userIdUtil.getIdentity(userName);
        if (identity==1){
            sendHomeWork.setHomeworkId(homeworkId);
            sendHomeWork.setUserId(userId);
            sendHomeWork.setNickname(nickname);
            sendHomeWork.setHeaderUrl(headerUrl);
            sendHomeWork.setCreateTime(new Date());
            int insert = sendHomeWorkMapper.insert(sendHomeWork);
            if (insert>0){
                return Result.success(nickname+","+"你已提交成功！");
            }
            return Result.failed("提交失败！");
        }
        return Result.failed("请登录！");
    }
}
